const app = getApp()
Page({
    data: {
        classification: false,
        tokenID: '',
        moment: [],
        datamsg: false,
        page: 1,
        momentType: '1002',
        momentName: '全部',
        deleteSysid: '',
        deleteIndex: 0,
        maskShow: false
    },
    onLoad: function(options) {
        var _this = this;
        _this.setData({
            tokenID: app.data.tokenID
        })
        _this.onReachBottom();
    },
    onPullDownRefresh: function() {
        var _this = this;
        this.setData({
            page: 1
        })
        wx.showNavigationBarLoading();
        _this.gtemomentList("down")
    },
    onReachBottom: function() {
        var that = this;
        wx.showLoading({
            title: '玩命加载中',
        })
        that.gtemomentList("up")
    },
    gtemomentList: function(type) {
        var that = this;
        that.setData({
            tokenID: app.data.tokenID
        })
        wx.request({
            url: app.data.api + '/questions/getmyaskquestionspage',
            data: {
                "page": that.data.page,
                "token": that.data.tokenID,
                "type": that.data.momentType
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (type == 'up') {
                    var moment_list = that.data.moment;

                    for (var i = 0; i < res.data.data.list.length; i++) {
                        var json = {};
                        var str = res.data.data.list[i].createTime,
                            createTime = str.substr(6, 4);
                        var imglist = [];
                        for (var j = 0; j < res.data.data.list[i].imgs.length; j++) {
                            var imgs = {}
                            imgs.src = res.data.data.list[i].imgs[j];
                            imgs.index = i
                            imglist.push(imgs)
                        }
                        json.content = res.data.data.list[i].content,
                            json.title = res.data.data.list[i].title,
                            json.createTime = createTime;
                        json.imgArr = imglist;
                        json.realName = res.data.data.list[i].realName;
                        json.header = res.data.data.list[i].header;
                        json.views = res.data.data.list[i].views;
                        json.sysid = res.data.data.list[i].sysid;
                        json.delete = res.data.data.list[i].delete;
                        json.commentsNum = res.data.data.list[i].commentsNum;
                        moment_list.push(json);
                    }
                    if (res.data.data.list.length == 0) {
                        that.setData({
                            datamsg: true
                        })
                    }
                    that.setData({
                        page: that.data.page + 1,
                        moment: that.data.moment
                    })
                } else {
                    var moment_list = [];

                    for (var i = 0; i < res.data.data.list.length; i++) {
                        var json = {};
                        var str = res.data.data.list[i].createTime,
                            createTime = str.substr(6, 4);
                        var imglist = [];
                        for (var j = 0; j < res.data.data.list[i].imgs.length; j++) {
                            var imgs = {}
                            imgs.src = res.data.data.list[i].imgs[j];
                            imgs.index = i
                            imglist.push(imgs)
                        }
                        json.content = res.data.data.list[i].content,
                            json.title = res.data.data.list[i].title,
                            json.createTime = createTime;
                        json.imgArr = imglist;
                        json.realName = res.data.data.list[i].realName;
                        json.header = res.data.data.list[i].header;
                        json.views = res.data.data.list[i].views;
                        json.sysid = res.data.data.list[i].sysid;
                        json.delete = res.data.data.list[i].delete;
                        json.commentsNum = res.data.data.list[i].commentsNum;
                        moment_list.push(json);
                    }
                    that.setData({
                        page: that.data.page + 1,
                        moment: moment_list
                    })
                    wx.stopPullDownRefresh();
                }
                wx.hideLoading();
                wx.hideNavigationBarLoading();

            }
        })
    },
    showClassification: function() {
        this.setData({
            classification: true
        })
    },
    hideClassification: function() {
        this.setData({
            classification: false
        })
    },
    imgshow: function(event) {
        var _this = this;
        var src = event.currentTarget.dataset.src;
        var index = event.currentTarget.dataset.index;
        var imgs = _this.data.moment[index].imgArr;
        var imgList = [];
        for (var i = 0; i < imgs.length; i++) {
            imgList.push(imgs[i].src)
        }
        wx.previewImage({
            current: src,
            urls: imgList
        })
    },
    screening: function(event) {
        var _this = this;
        _this.setData({
            momentType: event.currentTarget.dataset.momenttype,
            momentName: event.currentTarget.dataset.momentName,
            page: 1
        })
        _this.hideClassification();
        _this.gtemomentList("down")
    },
    deleteTopic: function(event) {
        var _this = this;
        var sysid = event.currentTarget.dataset.sysid;
        var index = event.currentTarget.dataset.index;
        _this.setData({
            deleteSysid: sysid,
            deleteIndex: index,
            maskShow: true
        })
    },
    cancel: function() {
        this.setData({
            maskShow: false
        })
    },
    ConfirmRemove: function() {
        var _this = this;
        var imgList = _this.data.moment;
        wx.showToast({
            title: '删除中...',
            icon: 'loading',
            mask: true,
            duration: 10000
        })
        wx.request({
            url: app.data.api + '/questions/deletequestions',
            data: {
                "token": _this.data.tokenID,
                "sysid": _this.data.deleteSysid
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status) {
                    wx.hideToast();
                    _this.onPullDownRefresh();
                    _this.cancel();
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        image: '../../images/warning.png',
                        duration: 800,
                    })
                }
            }
        })
    }
})